<template>
	<div id="basicInformation">
		<el-row>
			<el-col :span="4" class="backgroundTitelBox">
				比例信息
			</el-col>
			<el-col :span="2" class="refrestBox">
				<el-button
					type="primary"
					icon="el-icon-refresh"
					@click="handleEditTeam(false)"
				>
					{{ $t('common.refresh') }}
				</el-button>
			</el-col>
		</el-row>

		<div style="margin-top: 10px;" class="point-table">
			<AgentRebates
				:agentData="agentData"
				:isShow="isShow"
				:commissionModel="agentData.commissionModel"
				:queryRebateRateParams="queryRebateRateParams"
				:queryCommissionRateParams="queryCommissionRateParams"
				@agentPointSetAllSubOk="
					(data) =>
						agentData.commissionModel === 2
							? commissionRateUpdate(data)
							: agentPointSetAllSubOk(data)
				"
				:isEdit="isEdit"
				ref="AgentRebates"
			>
				<div
					v-if="
						hasPermission('90210042') && agentData.proxyLevel === 0
					"
					slot="otherCommission"
					class="AgentEditBtn editMsg"
				>
					<template v-if="isEdit.length !== 2">
						<el-button
							icon="el-icon-edit-outline"
							@click="
								handleEditTeam([
									'commissionDzRebates',
									'otherCommission'
								])
							"
						>
							编辑
						</el-button>
					</template>
					<div v-if="isEdit && isEdit.length === 2">
						<el-button
							type="primary"
							:disabled="loading"
							@click="handleSubmit()"
							:loading="loading"
						>
							{{ $t('common.submit') }}
						</el-button>
						<el-button @click="handleEditTeam(false)">
							取消
						</el-button>
					</div>
					<el-divider></el-divider>
				</div>

				<div
					v-if="
						hasPermission('90210042') && agentData.proxyLevel === 0
					"
					slot="teamContribution"
					class="AgentEditBtn editMsg"
				>
					<template v-if="isEdit.length !== 3">
						<el-button
							icon="el-icon-edit-outline"
							@click="
								handleEditTeam([
									'dzRebates',
									'teamRebates',
									'teamContribution'
								])
							"
						>
							编辑
						</el-button>
					</template>
					<div v-if="isEdit && isEdit.length === 3">
						<el-button
							type="primary"
							:disabled="loading"
							@click="handleSubmit()"
							:loading="loading"
						>
							{{ $t('common.submit') }}
						</el-button>
						<el-button @click="handleEditTeam(false)">
							取消
						</el-button>
					</div>
					<el-divider></el-divider>
				</div>

				<div
					v-if="
						hasPermission('90210042') && agentData.proxyLevel === 0
					"
					slot="memberPlatform"
					class="teamContributionBtn editMsg"
				>
					<template v-if="isEdit.length !== 1">
						<el-button
							icon="el-icon-edit-outline"
							@click="handleEditTeam(['memberPlatform'])"
						>
							编辑
						</el-button>
					</template>
					<div v-if="isEdit && isEdit.length === 1">
						<el-button
							type="primary"
							:disabled="loading"
							@click="handleSubmit()"
							:loading="loading"
						>
							{{ $t('common.submit') }}
						</el-button>
						<el-button @click="handleEditTeam(false)">
							取消
						</el-button>
					</div>
				</div>
			</AgentRebates>
		</div>

		<el-dialog
			:visible.sync="memberPlatformVisible"
			width="350px"
			:title="
				agentData.commissionModel === 1
					? '修改返点信息'
					: '修改返水信息'
			"
		>
			<p v-if="agentData.commissionModel === 1">
				确认修改返点信息后，修改前代理团队返点比例将生成已失效记录。未自定义设置过平台发放返水比例的会员将同步更新。您可勾选并同步更新所有代理条线会员的平台发放返水比例。
			</p>
			<p>
				确认修改返水信息后，未自定义设置过返水比例的会员将同步更新。您可勾选并同步更新所有代理条线会员的返水比例。
			</p>
			<div style="margin-top:25px">
				<el-checkbox v-model="isAllSub">
					{{
						agentData.commissionModel === 1
							? '同步更新所有会员平台发放返点比例'
							: '同步更新所有会员返水比例'
					}}
				</el-checkbox>
			</div>
			<span slot="footer" class="dialog-footer">
				<el-button @click="handleEditTeam(false)">取 消</el-button>
				<el-button
					type="primary"
					@click="fetchMemberPlatform()"
					:loading="loading"
				>
					确 定
				</el-button>
			</span>
		</el-dialog>
	</div>
</template>
<script>
import list from '@/mixins/list'
import AgentRebates from '@/components/AgentRebates'

export default {
	name: 'RebateInfo',
	components: {
		AgentRebates
	},
	mixins: [list],
	props: {
		agentData: {
			type: Object,
			default: () => ({})
		},
		outlineInfo: {
			type: Object,
			default: () => ({})
		}
	},
	data() {
		return {
			isEdit: false,

			memberPlatformVisible: false,
			isAllSub: false,
			memberPlatformConfig: {
				query: {
					platformFlag: 1,
					userType: 2,
					zhanchengFlag: 0
				}
			}
		}
	},
	computed: {
		queryRebateRateParams() {
			return {
				merchantId: this.agentData.merchantId,
				userName: this.agentData.userName,
				userId: this.agentData.id
				// userType: 0
				// status: 3
			}
		},
		queryCommissionRateParams() {
			return {
				merchantId: this.agentData.merchantId,
				userName: this.agentData.userName,
				userId: this.agentData.id
			}
		},
		isShow() {
			const names = [
				'dzRebates',
				'teamRebates',
				'teamContribution',
				'memberPlatform',
				'commissionDzRebates',
				'otherCommission'
			]
			// 只有总代显示平台默认会员返水
			if (this.agentData.proxyLevel > 0) {
				names.pop()
			}
			return names
		}
	},
	watch: {
		'agentData.userName'() {
			setTimeout(() => {
				this.$refs['AgentRebates'].queryRebateRate()
				if (this.agentData.commissionModel === 2) {
					this.$refs['AgentRebates'].queyrCommissionRate()
				}
			}, 100)
		}
	},
	created() {},
	methods: {
		handleEditTeam(namsOrBool) {
			this.memberPlatformVisible = false
			this.isEdit = namsOrBool
			if (namsOrBool === false) {
				this.$refs['AgentRebates'].queryRebateRate()
				console.log(this.agentData.commissionModel)
				if (this.agentData.commissionModel === 2) {
					this.$refs['AgentRebates'].queyrCommissionRate()
				}
			}
		},
		async handleClose(userType) {
			await this.$refs['AgentRebates'].validate()
		},
		async handleSubmit(name) {
			try {
				await this.$refs['AgentRebates'].validate()
				// 更新佣金信息
				// if (this.agentData.commissionModel === 2 && this.isEdit.includes('commissionDzRebates')) {
				// 	this.commissionRateUpdate()
				// }
				// 更新返点信息
				if (
					this.isEdit.includes('teamRebates') ||
					this.isEdit.includes('commissionDzRebates')
				) {
					this.$refs['AgentRebates'].agentPointSetAllSubVisible = true
				}

				// 更新会员返点
				if (this.isEdit.includes('memberPlatform')) {
					this.memberPlatformVisible = true
				}
			} catch (err) {
				throw err
			}
			// this.refs['AgentRebates'].upadteRebateRate(['teamRebates', 'teamContribution',])
		},
		// 提交佣金返点
		async commissionRateUpdate({ adjustmentType }) {
			const agentRebatesRef = this.$refs['AgentRebates']
			try {
				agentRebatesRef.agentPointSetAllSubLoading = true
				await this.$api.commissionRateUpdate({
					commissionRateList: this.$refs['AgentRebates'].getValues([
						'commissionDzRebates'
					]),
					merchantId: this.agentData.merchantId,
					userId: this.agentData.id,
					adjustmentType
				})
				agentRebatesRef.agentPointSetAllSubVisible = false
				this.handleEditTeam(false)
				this.$message.success(this.$t('common.save_success'))
			} catch (err) {
				console.error(err)
			} finally {
				agentRebatesRef.agentPointSetAllSubLoading = false
			}
		},

		// 提交团队返点
		async agentPointSetAllSubOk({ adjustmentType }) {
			const params = {
				adjustmentType,
				commissionMode: this.agentData.commissionModel,
				rebateAllotMode: 1,
				isAllSub: this.isAllSub,
				topProxyId: this.agentData.topProxyId,
				userId: this.agentData.topProxyId,
				userType: 1,
				// 同时更新（德州 - 保险团队占成）
				rebateRateList: this.$refs['AgentRebates'].getValues([
					'teamRebates',
					'teamContribution'
				])
			}

			const agentRebatesRef = this.$refs['AgentRebates']
			try {
				agentRebatesRef.agentPointSetAllSubLoading = true
				await agentRebatesRef.upadteRebateRate(params)
				// 弹出修改返点信息弹框
				agentRebatesRef.agentPointSetAllSubVisible = false
				this.isEditTeamRebates = false
				this.$message.success(this.$t('common.save_success'))
				this.handleEditTeam(false)
			} catch (err) {
				throw err
			} finally {
				agentRebatesRef.agentPointSetAllSubLoading = false
			}
		},

		// 更新会员返点
		async fetchMemberPlatform() {
			const params = {
				commissionMode: this.agentData.commissionModel,
				isAllSub: this.isAllSub,
				// topProxyId: this.agentData.parentProxyId,
				// userId: this.agentData.parentProxyId,
				// userType: 0,
				rebateAllotMode: 1,
				rebateRateList: this.$refs['AgentRebates'].getValues([
					'memberPlatform'
				])
			}
			const agentRebatesRef = this.$refs['AgentRebates']
			try {
				this.memberPlatformVisible = false
				await agentRebatesRef.upadteRebateRate(params)
				this.$message.success(this.$t('common.save_success'))
				this.isEditMemberPlatform = false
				this.handleEditTeam(false)
			} catch (err) {
				throw err
			} finally {
				agentRebatesRef.agentPointSetAllSubLoading = false
			}
		}
	}
}
</script>

<style lang="scss" scoped>
#basicInformation {
	font-size: 14px;
	line-height: 40px;
	padding-top: 10px;
}
::v-deep .el-dialog__header {
	text-align: center;
	color: #909399;
	font-weight: 700;
}
::v-deep .el-table--enable-row-hover .el-table__body tr:hover > td {
	background-color: #fff !important;
}

.editMsg {
	line-height: 18px;
	i {
		font-size: 18px;
		font-weight: bold;
		margin-bottom: 5px;
	}
}

.titelBox {
	font-size: 16px;
	line-height: 40px;
	font-weight: bold;
	position: relative;
}
.backgroundTitelBox {
	width: 188px;
	height: 40px;
	text-align: center;
	background: #000;
	color: rgb(255, 255, 255);
}
.refrestBox {
	text-align: center;
	display: flex;
	margin-left: 10px;
}
.clear {
	clear: both;
	height: 0;
	line-height: 0;
	font-size: 0;
}

.review-content {
	display: flex;
	margin-top: 20px;
}

.review-content span {
	min-width: 120px;
}

.point-table {
	display: inline-block;
	position: relative;
	min-height: 600px;
	margin-bottom: 50px;
	// .editMsg {
	// 	position: absolute;
	// 	top: 65px;
	// 	left: 1100px;
	// 	> a{
	// 		display: block;
	// 		width: 30px;
	// 	}
	// 	> div{
	// 		width: 155px;
	// 	}
	// }
}
</style>
